package com.anolesoft.yr.mbus.base.service.impl;

import com.anolesoft.yr.mbus.base.dto.BaseAddressBookDTO;
import com.anolesoft.yr.mbus.base.entity.BaseAddressBookDO;
import com.anolesoft.yr.mbus.base.mapper.BaseAddressBookMapper;
import com.anolesoft.yr.mbus.base.service.BaseAddressBookService;
import com.anolesoft.yr.util.CommonUtils;
import com.anolesoft.yr.util.CustomBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author zhj
 * @date 2020/9/12 17:26
 */
@Service
public class BaseAddressBookServiceImpl implements BaseAddressBookService {
    @Autowired
    private BaseAddressBookMapper baseAddressBookMapper;

    @Override
    public int create(BaseAddressBookDTO baseAddressBookDTO) {
        return baseAddressBookMapper.insert(BaseAddressBookDO.builder()
                .createUserUid(baseAddressBookDTO.getCreateUserUid())
                .deleteFlag(false)
                .dept(baseAddressBookDTO.getDept())
                .email(baseAddressBookDTO.getEmail())
                .factoryCode(baseAddressBookDTO.getFactoryCode())
                .modifyUserUid(baseAddressBookDTO.getModifyUserUid())
                .name(baseAddressBookDTO.getName())
                .objuid(CommonUtils.fastUUID())
                .phone(baseAddressBookDTO.getPhone())
                .post(baseAddressBookDTO.getPost())
                .remark(baseAddressBookDTO.getRemark())
                .targetUid(baseAddressBookDTO.getTargetUid())
                .type(baseAddressBookDTO.getType())
                .build());
    }

    @Override
    public int modify(BaseAddressBookDTO baseAddressBookDTO) {
        BaseAddressBookDO baseAddressBookDO = baseAddressBookMapper.selectOneById(baseAddressBookDTO.getObjuid());
        if(baseAddressBookDO == null){
            baseAddressBookDO = BaseAddressBookDO.builder().build();
        }
        baseAddressBookDO.setCreateUserUid(baseAddressBookDTO.getCreateUserUid());
        baseAddressBookDO.setDeleteFlag(baseAddressBookDTO.getDeleteFlag());
        baseAddressBookDO.setDept(baseAddressBookDTO.getDept());
        baseAddressBookDO.setEmail(baseAddressBookDTO.getEmail());
        baseAddressBookDO.setFactoryCode(baseAddressBookDTO.getFactoryCode());
        baseAddressBookDO.setModifyUserUid(baseAddressBookDTO.getModifyUserUid());
        baseAddressBookDO.setName(baseAddressBookDTO.getName());
        baseAddressBookDO.setObjuid(baseAddressBookDTO.getObjuid());
        baseAddressBookDO.setPhone(baseAddressBookDTO.getPhone());
        baseAddressBookDO.setPost(baseAddressBookDTO.getPost());
        baseAddressBookDO.setRemark(baseAddressBookDTO.getRemark());
        baseAddressBookDO.setTargetUid(baseAddressBookDTO.getTargetUid());
        baseAddressBookDO.setType(baseAddressBookDTO.getType());
        return baseAddressBookMapper.update(baseAddressBookDO);
    }

    @Override
    public int remove(String id) {
        return baseAddressBookMapper.delete(id);
    }

    @Override
    public int removeBatch(List<String> ids) {
        int res = 0;
        for (String id : ids) {
            res += baseAddressBookMapper.delete(id);
        }
        return res;
    }

    @Override
    public List<BaseAddressBookDTO> selectAll() {
        return baseAddressBookMapper.selectAll().stream().map(t-> CustomBeanUtils.baseAddressBookDO2DTO(t)).collect(Collectors.toList());
    }

    @Override
    public Optional<BaseAddressBookDTO> selectOneById(String id) {
        BaseAddressBookDO baseAddressBookDO = baseAddressBookMapper.selectOneById(id);
        if (baseAddressBookDO != null) {
            return Optional.of(CustomBeanUtils.baseAddressBookDO2DTO(baseAddressBookDO));
        }
        return Optional.empty();
    }

    @Override
    public List<BaseAddressBookDTO> selectListByTargetUid(String targetUid) {
        return baseAddressBookMapper.selectListByTargetUid(targetUid).stream().map(t-> CustomBeanUtils.baseAddressBookDO2DTO(t)).collect(Collectors.toList());
    }
}
